package com.houzehui.product.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houzehui.product.config.JwtUtils;
import com.houzehui.product.config.MsgResult;
import com.houzehui.product.config.WebUtils;
import com.houzehui.product.dto.SpuQueryDto;
import com.houzehui.product.entity.Brand;
import com.houzehui.product.entity.Sku;
import com.houzehui.product.entity.User;
import com.houzehui.product.service.IBrandService;
import com.houzehui.product.service.ISkuService;
import com.houzehui.product.service.ISpuService;
import com.houzehui.product.vo.SkuDteailVo;
import com.houzehui.product.vo.SpecParamVoo;
import com.houzehui.product.vo.SpuListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * spu表，该表描述的是一个抽象性的商品，比如 iphone8 前端控制器
 * </p>
 *
 * @author houzehui
 * @since 2022-04-27
 */
@RestController
@RequestMapping("/spu")
public class SpuController {

    @Autowired
    ISpuService iSpuService;
    @Autowired
    ISkuService iSkuService;

    @Autowired
    IBrandService iBrandService;

    @GetMapping("list")
    public MsgResult list(SpuQueryDto spuQueryDto, @RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "4")int pageSize){


        PageInfo<SpuListVo> spuListVoPageInfo1 = iSpuService.selectSpuList(spuQueryDto, pageNum, pageSize);



        return MsgResult.succeed(spuListVoPageInfo1);
    }

    @GetMapping("getDetailBySkuId/{skuId}")
    public MsgResult getDetailBySkuId(@PathVariable("skuId") Long skuId){
        System.err.println(skuId);
        SkuDteailVo detailBySkuId = iSkuService.getDetailBySkuId(skuId);

        return  MsgResult.succeed(detailBySkuId);
    }





    @GetMapping("getbrand")
    public MsgResult getbrand(){

        List<Brand> list = iBrandService.list();
        return MsgResult.succeed(list);
    }

    @Autowired
    JwtUtils jwtUtils;
    @GetMapping("getuserId")
    public MsgResult getUserId(){
        String header = WebUtils.getRequest().getHeader("token");
        String subject = jwtUtils.getTokenClaim(header).getSubject();

        User user = JSON.parseObject(subject, User.class);

        return MsgResult.succeed(user.getId());
    }




}
